<?php
namespace app\admin\controller;
use think\AjaxPage;
use think\Controller;
use think\Url;
use think\Config;
use think\Page;
use think\Verify;
use think\Db;
use think\Request;
use app\admin\model\Goods as goodsModel;
use app\admin\model\Goods_category as categoryModel;
use app\admin\model\Goods_brand as brandModel;
use app\admin\model\Pictures as picsModel;
class Goods extends Controller
{
    //商品列表页
    public function index()
    {  
        $goodsRes=goodsModel::all();
        $this->assign('goodsList',$goodsRes);
    	return $this->fetch();
    }
     /**
     * 添加商品
     *2017-6-43
     */
    public function test(){
        
        var_dump($_FILES);
    }
    public function goods_add(Request $request){
        //判断是否有数据提交
        if($request->param()){
            //获取上传数据数组
            $res = $request->param();
            // if (empty($res['name'])) {
            //     $this->error('没有填写任何信息');
            // }
            //循环判断数组内是否为数字,如果是放入新数组
            for($i=1;$i<=3;$i++){
               if(is_numeric($res['cid_'.$i])){
                    $cidArr[]=$res['cid_'.$i];
               }
            }
            if(!empty($cidArr)){
                $res['cid']=array_pop($cidArr);
            }
            //检测商品图是否存在
            $files = $request->file('file');
            if (empty($files)) {
                $this->error('最少也要选择一个商品的图,求真相');
            }               
            //准备商品数据
            $insertData=array(                    
                'cat_id'=>$res['cid'],
                'brand_id'=>$res['brank'],
                'name'=>$res['name'],
                'price'=>$res['price'],
                'stock'=>$res['stock'],
                'describe'=>$res['goods_desc'],
                'goodsinfo'=>$res['goods_info'],
                'type'=>$res['goods_type'],
                'is_shelf'=>$res['is_shelf'],
                'on_time'=>time(),
            );            
            //写入关联数据库
            if ($goodsRes=GoodsModel::create($insertData)) {
                //处理图片数据
                foreach($files as $file){
                    // 移动到框架应用根目录/public/uploads/ 目录下
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'thumb');
                    //对图片进行处理
                    $image= \think\Image::open($file);
                    //生成大中小三种图片
                    $image->thumb(800,800)->save(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'thumb_big'. DS .$info->getFileName());
                    $image->thumb(500,500)->save(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'thumb_mid'. DS .$info->getFileName());
                    $image->thumb(150, 150)->save(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'thumb_sm'. DS .$info->getFileName());
                    $spec=explode('/',$info->getinfo('type'));
                    if($info){
                        // 成功上移动了 图片数据,向数据库添加
                        $fileImgData=array(
                            'album_id'=>$goodsRes->id,
                            'pic_name'=>$info->getinfo('name'),
                            'pic_cover'=>DS . 'uploads' . DS . 'thumb'. DS .$info->getSaveName(),
                            'pic_size'=>$info->getinfo('size'),
                            'pic_spec'=>$spec[1],
                            'pic_cover_big'=>DS . 'uploads' . DS . 'thumb_big'. DS .$info->getFileName(),
                            'pic_cover_mid'=>DS . 'uploads' . DS . 'thumb_big'. DS .$info->getFileName(),
                            'pic_cover_small'=>DS . 'uploads' . DS . 'thumb_sm'. DS .$info->getFileName(),
                            'upload_time'=>time()
                            );
                        picsModel::create($fileImgData);
                    }else{
                        // 上传失败获取错误信息
                        echo $file->getError();
                    }
                }
                return $this->success('添加 '.$goodsRes->name.' 商品成功了');
            }
        }
        //没有数据提交
        $tableGoodsArr=new categoryModel;
        $goods_category_menu=$tableGoodsArr->getmenu(0);
        $goods_brand_menu=brandModel::all();
        $this->assign('categoryList',$goods_category_menu);
        $this->assign('brandList',$goods_brand_menu);
        return $this->fetch(); 
    }
    //商品品牌列表页
    public function goods_brand(){
        $brandRes=brandModel::select();
        $this->assign('brandList',$brandRes);
        return $this->fetch();
    }
    //添加商品品牌
    public function brand_add(Request $request){
        if($res=$request->param()){
            //获取表单上传文件
            $file = $request->file('logo');
            if (empty($file)) {
                $this->error('请选择上传文件');
            }
            $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'upload' . DS . 'brand');
            if ($info) {
                $filepath='upload' . DS . 'brand'. DS .$info->getSaveName();
                $res['logo']=$filepath;
                if ($brandRes=brandModel::create($res)) {
                    return $this->success('添加品牌'.$brandRes->name.'成功了');
                }
            } else {
                // 上传失败获取错误信息
                $this->error($file->getError());
             }
        }
        $tableGoodsArr=new categoryModel;
        $goods_category_menu=$tableGoodsArr->getmenu(0);
        $this->assign('categoryList',$goods_category_menu);
        return $this->fetch();
    }
    //商品分类主页列表
    public function category(Request $request){
        if($res=$request->param()){            
            $catData=new categoryModel;
            if ($res['pid']==0) {
                $res['level']=1;
            }
            if ($catres=$catData->create($res)) {
                return $this->success('用户'.$catres->id.'成功了');
            }
        }else{
            $tableGoodsArr=new categoryModel;
            $goods_category_menu=$tableGoodsArr->getmenu();
            $this->assign('category_menu',$goods_category_menu);
            return $this->fetch();
        }       
    }
    //添加商品分类
    public function addCategory(Request $request){
        if($res=$request->param()){
            //判断是否为顶级目录
            if ($res['pid']!=0) {
                //非顶级目录的话
                $res['path']=categoryModel::get($res['pid'])['path'].'_'.$res['pid'];
                $res['level']=categoryModel::get($res['pid'])['level']+1;
            }else{
                //顶级目录
                $res['level']=1;
            }
            //写入数据
            if($catres=categoryModel::create($res)){
                return $this->success('栏目 '.$catres->cname.'添加成功','category');
            }
        }
        $tableGoodsArr=new categoryModel;
        $goods_category_menu=$tableGoodsArr->getmenu();
        $this->assign('cat_list',$goods_category_menu);
        return $this->fetch();
    }
    //获取分类
    public function getCateTwo(Request $request){
        $pid=$request->get('pid');
        $tableGoodsArr=new categoryModel;
        $cateNameArr=$tableGoodsArr->getmenu($pid);
        echo "<option>选择子类</option>";
        foreach ($cateNameArr as $row) {
            echo "<option value=".$row['id'].">".$row['cname']."</option>";
        }        
    }
}
